package com.cleaningcloud.cld.service.impl;

import java.util.Date;
import java.util.List;

import com.cleaningcloud.cld.domain.CldTrainReceive;
import com.cleaningcloud.cld.domain.dto.CldTrainPlanDto;
import com.cleaningcloud.cld.domain.vo.CldTrainPlanVo;
import com.cleaningcloud.cld.mapper.CldTrainReceiveMapper;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.StringUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.cld.mapper.CldTrainPlanMapper;
import com.cleaningcloud.cld.domain.CldTrainPlan;
import com.cleaningcloud.cld.service.ICldTrainPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 培训计划Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2024-10-25
 */
@Service
public class CldTrainPlanServiceImpl implements ICldTrainPlanService 
{
    @Autowired
    private CldTrainPlanMapper cldTrainPlanMapper;

    @Autowired
    private CldTrainReceiveMapper trainReceiveMapper;

    /**
     * 查询培训计划
     * 
     * @param trainPlanId 培训计划主键
     * @return 培训计划
     */
    @Override
    public CldTrainPlanVo selectCldTrainPlanByTrainPlanId(Long trainPlanId)
    {
        CldTrainPlanVo cldTrainPlanVo = cldTrainPlanMapper.selectCldTrainPlanByTrainPlanId(trainPlanId);
        if(StringUtils.isEmpty(cldTrainPlanVo.getTrainAuditorName())){
            cldTrainPlanVo.setTrainAuditorName(cldTrainPlanVo.getTrainAuditorUName());
        }
        // 查询受训人列表
        cldTrainPlanVo.setTraineesList(trainReceiveMapper.selectCldTrainReceiveListByPlanId(cldTrainPlanVo.getTrainPlanId()));
        return cldTrainPlanVo;
    }

    /**
     * 查询培训计划列表
     * 
     * @param cldTrainPlan 培训计划
     * @return 培训计划
     */
    @Override
    public List<CldTrainPlanVo> selectCldTrainPlanList(CldTrainPlanDto cldTrainPlan)
    {
        List<CldTrainPlanVo> list = cldTrainPlanMapper.selectCldTrainPlanList(cldTrainPlan);
        for (CldTrainPlanVo cldTrainPlanVo : list) {
            if(StringUtils.isEmpty(cldTrainPlanVo.getTrainAuditorName())){
                cldTrainPlanVo.setTrainAuditorName(cldTrainPlanVo.getTrainAuditorUName());
            }
        }
        return list;
    }

    /**
     * 新增培训计划
     * 
     * @param cldTrainPlan 培训计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldTrainPlan(CldTrainPlanDto cldTrainPlan)
    {
        cldTrainPlan.setCreateTime(DateUtils.getNowDate());
        cldTrainPlan.setCreateId(SecurityUtils.getUserId());
        cldTrainPlan.setCreateName(SecurityUtils.getUsername());
        cldTrainPlan.setFlag(0L);
        cldTrainPlan.setTrainStatus(0L);
        cldTrainPlanMapper.insertCldTrainPlan(cldTrainPlan);
        // 根据时间判断 如培训开始时间小于当日 结束时间大于当日 则味进行
        if(cldTrainPlan.getStartTime().compareTo(new Date()) >= 0){
            cldTrainPlan.setTrainStatus(1L);
        }else{
            cldTrainPlan.setTrainStatus(0L);
        }
        // 新增受训人信息
        if(!cldTrainPlan.getTraineesList().isEmpty()){
            cldTrainPlan.getTraineesList().forEach(item -> {
                item.setIecturerId(cldTrainPlan.getTrainIecturerId());
                item.setTrainPlanId(cldTrainPlan.getTrainPlanId());
            });
            trainReceiveMapper.batchCldTrainReceive(cldTrainPlan.getTraineesList());
        }
        return 1;
    }

    /**
     * 修改培训计划
     * 
     * @param cldTrainPlan 培训计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCldTrainPlan(CldTrainPlanDto cldTrainPlan)
    {
        trainReceiveMapper.deleteCldTrainReceiveByPlanId(cldTrainPlan.getTrainPlanId());
        // 根据时间判断 如培训开始时间小于当日 结束时间大于当日 则味进行
        if(cldTrainPlan.getStartTime().compareTo(new Date()) >= 0){
            cldTrainPlan.setTrainStatus(1L);
        }else{
            cldTrainPlan.setTrainStatus(0L);
        }
        // 新增受训人信息
        if(!cldTrainPlan.getTraineesList().isEmpty()){
            for (CldTrainReceive cldTrainReceive : cldTrainPlan.getTraineesList()) {
                cldTrainReceive.setIecturerId(cldTrainPlan.getTrainIecturerId());
            }
            trainReceiveMapper.batchCldTrainReceive(cldTrainPlan.getTraineesList());
        }
        return cldTrainPlanMapper.updateCldTrainPlan(cldTrainPlan);
    }

    /**
     * 批量删除培训计划
     * 
     * @param trainPlanIds 需要删除的培训计划主键
     * @return 结果
     */
    @Override
    public int deleteCldTrainPlanByTrainPlanIds(Long[] trainPlanIds)
    {
        return cldTrainPlanMapper.deleteCldTrainPlanByTrainPlanIds(trainPlanIds);
    }

    /**
     * 删除培训计划信息
     * 
     * @param trainPlanId 培训计划主键
     * @return 结果
     */
    @Override
    public int deleteCldTrainPlanByTrainPlanId(Long trainPlanId)
    {
        return cldTrainPlanMapper.deleteCldTrainPlanByTrainPlanId(trainPlanId);
    }
}
